package com.team.mp.api.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.mp.api.bo.ServiceSearchBo;
import com.team.mp.api.bo.ServiceinfoSearchBo;
import com.team.mp.api.service.intf.ServiceinfoService;
import com.team.mp.mapper.dao.ServiceinfoDao;
import com.team.mp.mapper.dto.ServiceSearchDto;
import com.team.mp.mapper.dto.ServiceinfoSearchDto;
import com.team.mp.mapper.entity.Serviceinfo;
import com.team.mp.vo.R;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.team.mp.api.bo.ServiceinfoAddBo;
import com.team.mp.api.bo.ServiceinfoUpdateBo;
import com.team.mp.api.service.intf.ServiceinfoService;
import com.team.mp.mapper.dao.ServiceinfoAuditDao;
import com.team.mp.mapper.entity.Serviceinfo;
import com.team.mp.mapper.dao.ServiceinfoDao;
import javax.annotation.Resource;

import com.team.mp.mapper.entity.ServiceinfoAudit;
import com.team.mp.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: Lixiang L2Y LXR
 * @date: 2023/3/27 16:06
 * @description:
 * @version:
 */
@Service
public class ServiceinfoServiceImpl implements ServiceinfoService {
    /**
     * 注入持久层对象
     */
    @Resource
    private ServiceinfoDao dao;

//    /**
//     * 新增内容
//     */
//    @Override
//    public R save(Serviceinfo serviceinfo) {
//        if (serviceinfo != null) {
//            int i = dao.insert(serviceinfo);
//            if (i > 0) {

    @Resource
    private ServiceinfoAuditDao auditDao;

    /**
    * 新增内容*/
    @Override
    public R save(ServiceinfoAddBo bo) {
        if(bo != null){
            Serviceinfo serviceinfo = new Serviceinfo();
            serviceinfo.init();
            BeanUtils.copyProperties(bo, serviceinfo);
            int i = dao.insert(serviceinfo);
            int j = dao.updateSOrderInt(serviceinfo.getId());
//            int j = dao.update(null, new UpdateWrapper<Serviceinfo>().setSql("s_order=CONCAT('2023',LPAD(id, 8,0))").eq("id", serviceinfo.getId()));
            if(i > 0 && j > 0){
                return R.ok();
            }
        }
        return R.fail("亲，新增异常！");
    }

//    /**
//     * 修改内容
//     */
//    @Override
//    public R update(Serviceinfo serviceinfo) {
//        if (serviceinfo != null) {
//            int i = dao.updateById(serviceinfo);
//            if (i > 0) {
//                return R.ok();
//            }
//        }
//        return R.fail("亲，修改异常！");
//    }

    /**
     * 删除
     */
    @Override
    public R del(long id) {
        if (id > 0) {
            int i = dao.deleteById(id);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
     * 查询详情
     */
    @Override
    public R queryById(long id) {
        return R.ok(dao.selectById(id));
    }

    /**
     * 查询 所有数据
     */
    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }

    /**
     * 条件查询
     */
    @Override
    public R queryByCond(int page,int size,ServiceSearchBo bo) {

        return R.ok(dao.selectInfo(new Page<>(page, size), bo.getDepartmentId(),
                bo.getTeamId(),
                bo.getSerpackId(),
                bo.getSignStatus(),
                bo.getCtime()));
    }

    @Override
    public R queryByCond2(int page, int size, ServiceinfoSearchBo bo) {
        return R.ok(dao.selectListInfo(new Page<>(page, size),
                bo.getDoctorId(),
                bo.getSpId(),
                bo.getStatus()));
    }
    /**
    * 修改内容*/
    @Override
    public R update(ServiceinfoUpdateBo bo) {
        if(bo != null){
            Serviceinfo serviceinfo = new Serviceinfo();
            BeanUtils.copyProperties(bo, serviceinfo);
            int i = dao.updateById(serviceinfo);
             if(i > 0){
                return R.ok();
              }
        }
        return R.fail("亲，修改异常！");
    }

    /**
    * 查询详情*/
    @Override
    public R queryAuditBySId(int sid){
        return R.ok(dao.selectAuditById(sid));
    }

    /**
     * 查询取消详情2*/
    @Override
    public R queryCancelBySId(int sid){
        return R.ok(dao.selectCancelById(sid));
    }

    /**
     * 查询完成详情3*/
    @Override
    public R queryFinishedBySId(int sid){
        return R.ok(dao.selectFinishedById(sid));
    }
    @Override
    public R updateStatus(int id, int status) {
        UpdateWrapper<Serviceinfo> wrapper = new UpdateWrapper<>();
        wrapper.set("status", status).eq("id", id);
        int i = dao.update(null, wrapper);
        if (status == 2) {
            auditDao.insert(new ServiceinfoAudit(1, id, "驳回"));
        }
        if (status == 3) {
            auditDao.insert(new ServiceinfoAudit(1, id, "通过"));
        }
        if (i > 0) {
            return R.ok();
        }
        return R.fail("审核失败！");
    }
}
